package com.boot.hystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * Created by Wuxiang on 2018/5/9
 */
@Service
public class HelloService {

    private static Random random = new Random();

    private static Logger logger = LoggerFactory.getLogger(HelloService.class);

    @HystrixCommand(fallbackMethod = "fallBack")
    public String helloName(String username) {
        long sleep = random.nextInt(2000);
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            logger.error(e.toString());
        }
        return "hello1:" + username + " do something.";
    }

    @HystrixCommand(commandKey = "hello", fallbackMethod = "fallBack")
    public String hello(String username) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            logger.error(e.toString());
        }
        return "hello2:" + username + "do something.";
    }

    public String fallBack(String username) {
        return username + "fallback";
    }

}
